local jiumeng_bianshen_active = fk.CreateSkill {

  name = "jiumeng_bianshen_active",

  tags = { Skill.Limited, },

}



jiumeng_bianshen_active:addEffect("active", {
  name = "jiumeng_bianshen_active",
  anim_type = "special",
  prompt = "#jiumeng_bianshen_evoke",
  can_use = function (self, player)
    return player:usedSkillTimes(jiumeng_bianshen_active.name,Player.HistoryGame) == 0
  end,
  card_num = 0,
  card_filter = Util.FalseFunc,
  target_num = 0,
  target_filter = Util.FalseFunc,
  on_use = function (self, room, effect)
    local player = effect.from
    if player.general == "hxjsct__kakaluote" then
      room:setPlayerProperty(player,"general","jiumeng_kakaluote_juexing")
    elseif player.deputyGeneral and player.deputyGeneral == "hxjsct__kakaluote" then
      room:setPlayerProperty(player,"deputyGeneral","jiumeng_kakaluote_juexing")
    end
    room:recover{
      who = player,
      num = 5 - player.hp,
      recoverBy = player,
      skillName = jiumeng_bianshen_active.name,
    }
    room:changeShield(player,5)
    local mark = player:getMarkNames()
    if #mark > 0 then
      for _, v in ipairs(mark) do
        if v:startsWith("@") then
          room:setPlayerMark(player,v,0)
        end
      end
    end
    room:handleAddLoseSkills(player,"jiumeng_jiewangquan|jiumeng_qigong|jiumeng_yuanqi|jiumeng_zhanyi",nil,nil,true)
    local choices = {"jiumeng_bianshen_kongzhi","jiumeng_bianshen_shuchu","Cancel"}
    local choice = room:askToChoice(player,{ choices = choices, skill_name = jiumeng_bianshen_active.name, prompt = "#jiumeng_bianshen_choose", detailed = true, all_choices = choices})
    if choice == "jiumeng_bianshen_kongzhi" then
      local tos = room:askToChoosePlayers(player,{ targets = room.alive_players, min_num = 1, max_num = 8, prompt = "jiumeng_bianshen_kongzhi_choose", skill_name = jiumeng_bianshen_active.name,  cancelable = true, no_indicate = false})
      local getequips = {}
      for _, to in ipairs(tos) do
        local num = math.min(#to:getCardIds({Player.Hand,Player.Equip}),5)
        local discards = room:askToChooseCards(player,{ target = to, min = num, max = num, flag = "he", skill_name = jiumeng_bianshen_active.name, prompt = "#jiumeng_bianshen_kongzhi_discard::"..to.id})
        if #discards > 0 then
          room:throwCard(discards,jiumeng_bianshen_active.name,to,player)
        end
        for _, c in ipairs(to:getCardIds(Player.Equip)) do
          table.insertIfNeed(getequips,c)
        end
      end
      if #getequips > 0 then
        room:obtainCard(player,getequips,true,fk.ReasonPrey,player,jiumeng_bianshen_active.name)
        local finish = false
        while #getequips > 0 and finish == false do
          local use = room:askToUseRealCard(player,{ pattern = getequips, skill_name = jiumeng_bianshen_active.name, prompt = "#jiumeng_bianshen_kongzhi_use", cancelable = true, skip = true})
          if use then
            room:useCard(use)
            table.removeOne(getequips,use.card.id)
          else
            finish = true
          end
        end
      end
    elseif choice == "jiumeng_bianshen_shuchu" then
      local tos = room:askToChoosePlayers(player,{ targets = room.alive_players, min_num = 1, max_num = 8, prompt = "jiumeng_bianshen_shuchu_choose", skill_name = jiumeng_bianshen_active.name,  cancelable = true, no_indicate = false})
      for _, to in ipairs(tos) do
        room:damage{
          to = to,
          damage = 3,
          from = player,
          skillName = jiumeng_bianshen_active.name,
        }
      end
      player:drawCards(5,jiumeng_bianshen_active.name)
    end
    player:gainAnExtraTurn(false,jiumeng_bianshen_active.name)
  end,
})

jiumeng_bianshen_active:addEffect(fk.EnterDying, {
  name = "#jiumeng_bianshen",
  anim_type = "support",
  can_trigger = function (self, event, target, player, data)
    return player:hasSkill(jiumeng_bianshen_active.name) and target and target == player and player:usedSkillTimes("jiumeng_bianshen_active",Player.HistoryGame) == 0
  end,
  on_cost = function (self, event, target, player, data)
    local room = player.room
    player:setSkillUseHistory("jiumeng_bianshen_active",1,Player.HistoryGame)
    room:askToUseActiveSkill(player,{ skill_name = "jiumeng_bianshen_active", prompt = "#jiumeng_bianshen_evoke", cancelable = true})
  end,
})

return jiumeng_bianshen_active